import { post, get } from '@/utils/request';

// 后端返回的统一格式
export interface ResultBean<T> {
  code: number;
  message: string;
  data: T;
}

// 角色信息接口定义
export interface RoleInfo {
  roleId?: number;
  roleName: string;
  roleLevel: number;
  createTime?: number;
  updateTime?: number;
  funcList?: string[]; // 角色关联的菜单ID列表（funcId格式）
}

// 角色查询参数
export interface RoleQueryParams {
  pageNo?: number;
  pageSize?: number;
  roleName?: string;
  roleLevel?: number;
}

// 角色列表响应
export interface RoleListResponse {
  rows: RoleInfo[];
  page: {
    totalRow: number;
    pageNo: number;
    pageSize: number;
  };
}

// 菜单树节点接口
export interface MenuTreeNode {
  funcId: string;
  funcName: string;
  funcPath?: string;
  upFuncId?: string;
  index?: number;
  children?: MenuTreeNode[];
}

// 角色管理API
export function getRoleList(params: RoleQueryParams) {
  return post<ResultBean<RoleListResponse>>('/api/role/query', params);
}

export function getRoleById(roleId: number) {
  return post<ResultBean<RoleInfo>>('/api/role/queryById', roleId);
}

export function createRole(data: RoleInfo) {
  return post<ResultBean<RoleInfo>>('/api/role/add', data);
}

export function updateRole(data: RoleInfo) {
  return post<ResultBean<RoleInfo>>('/api/role/upd', data);
}

export function deleteRole(roleId: number) {
  return post<ResultBean<null>>('/api/role/del', roleId);
}

export function batchDeleteRoles(roleIds: number[]) {
  return post<ResultBean<null>>('/api/role/batch-del', { roleIds });
}

// 获取菜单树
export function getMenuTree() {
  return get<ResultBean<MenuTreeNode[]>>('/api/menu/tree');
}

// 获取所有菜单功能
export function getAllMenus() {
  return post<ResultBean<MenuTreeNode[]>>('/api/func/query', {});
}

export function queryRoleList() {
  return get<ResultBean<RoleInfo[]>>('/api/role/list');
}
